package com.youlai.boot.modules.content.controller;

import com.youlai.boot.modules.content.model.vo.LzCategoryVO;
import com.youlai.boot.modules.content.service.LzTagService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.youlai.boot.modules.content.model.form.LzTagForm;
import com.youlai.boot.modules.content.model.query.LzTagQuery;
import com.youlai.boot.modules.content.model.vo.LzTagVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

import java.util.ArrayList;

/**
 * 标签前端控制层
 *
 * @author lz006891
 * @since 2025-02-24 19:37
 */
@Tag(name = "标签接口")
@RestController
@RequestMapping("/api/v1/lzTags")
@RequiredArgsConstructor
public class LzTagController  {

    private final LzTagService lzTagService;

    @Operation(summary = "标签分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('content:lzTag:query')")
    public PageResult<LzTagVO> getLzTagPage(LzTagQuery queryParams ) {
        IPage<LzTagVO> result = lzTagService.getLzTagPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增标签")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('content:lzTag:add')")
    public Result<Void> saveLzTag(@RequestBody @Valid LzTagForm formData ) {
        boolean result = lzTagService.saveLzTag(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取标签表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('content:lzTag:edit')")
    public Result<LzTagForm> getLzTagForm(
        @Parameter(description = "标签ID") @PathVariable Long id
    ) {
        LzTagForm formData = lzTagService.getLzTagFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改标签")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('content:lzTag:edit')")
    public Result<Void> updateLzTag(
            @Parameter(description = "标签ID") @PathVariable Long id,
            @RequestBody @Validated LzTagForm formData
    ) {
        boolean result = lzTagService.updateLzTag(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除标签")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('content:lzTag:delete')")
    public Result<Void> deleteLzTags(
        @Parameter(description = "标签ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = lzTagService.deleteLzTags(ids);
        return Result.judge(result);
    }

    // 获取所有分类
    @Operation(summary = "获取所有标签")
    @GetMapping("/all")
    public Result<ArrayList<LzTagVO>> getAllLzTags() {
        return Result.success(lzTagService.getAllLzTags());
    }
}
